﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using mvc.Models.Database;
using mvc.Models;

namespace mvc.Controllers
{
    [Route("[controller]/[action]")]
    [Route("/[action].html")]
    
    
    public class TestController : Controller
    {
        private readonly MVCContext _db;
        public TestController(MVCContext db)
        {
            _db = db;
        }
        public IActionResult Index(int cateid,int newsid)
        {
            //查询新闻类名列表
            var newsclass = _db.NewsClasses.ToList();
            //传递新闻类名列表
            ViewData["newsclass"] = newsclass;
            if(cateid == 0)
            {
                cateid = newsclass.FirstOrDefault().Id;
            }
            //查询分类新闻列表
            var news = _db.News.Where(x => x.Id == cateid).ToList();
            //传递分类新闻列表
            ViewData["news"] = news;
            ViewData["cateid"] = cateid;
            return View();
        }
        public IActionResult detail(int cateid,int newsid)
        {
            //查询对应id新闻
            var newsID= _db.News.Where(x => x.Newsid == newsid).ToList();
            //传递id新闻
            ViewData["newsID"] = newsID;
            
            //查询新闻类名列表
            var newsclass = _db.NewsClasses.ToList();
            //传递新闻类名列表
            ViewData["newsclass"] = newsclass;
            if (cateid == 0)
            {
                cateid = newsclass.FirstOrDefault().Id;
            }
            //查询分类新闻列表
            var news = _db.News.Where(x => x.Id == cateid).ToList();
            ViewData["news"] = news;
            //传递分类新闻列表
            ViewData["cateid"] = cateid;
            return View();
        }
    }
}
